<template>
  <div id='param'>
    <div class="scroll-content" @scroll="onScroll" :style="
      'overflow-x: hidden; overflow-y: auto;'
    ">
      <el-form ref="form" :model="form" :inline="true" :rules="rules" label-position="top" label-width="125px">
        <!-- 注册信息 -->
        <div :ref="tabs[0].refName" class="scroll-item">
          <div class="scroll-item-title">
            <div class="scroll-item-title-before"></div>
            <div class="scroll-item-title-content">注册信息</div>
          </div>
          <div class="scroll-item-content">
            <el-form-item label="设备序列号" prop="code">
              <el-input v-model="form.code" disabled placeholder="请输入设备序列号" />
            </el-form-item>
            <el-form-item label="协议版本号" prop="version">
              <el-input v-model="form.version" disabled placeholder="请输入协议版本号" />
            </el-form-item>
            <!-- <el-form-item label="用户名" prop="userName">
              <el-input v-model="form.userName" disabled placeholder="请输入用户名" />
            </el-form-item>
            <el-form-item label="用户密码" prop="password">
              <el-input v-model="form.password" type="password" disabled placeholder="请输入用户密码" />
            </el-form-item> -->
            <!-- <el-form-item label="设备类型" prop="typeLabel">
              <el-input v-model="form.typeLabel" disabled placeholder="请输入设备类型" />
            </el-form-item> -->
            <el-form-item label="仪器类型" prop="typeLabel">
              <el-input v-model="form.typeLabel" disabled placeholder="请输入仪器类型" />
            </el-form-item>
            <!-- <el-form-item label="协议类型" prop="">
              <el-input v-model="form.netname" disabled placeholder="请输入台网名称" />
            </el-form-item> -->
          </div>
        </div>
        <!-- 静态信息 -->
        <div :ref="tabs[1].refName" v-if="register" class="scroll-item">
          <div class="scroll-item-title">
            <div class="scroll-item-title-before"></div>
            <div class="scroll-item-title-content">静态信息</div>
          </div>
          <div class="scroll-item-content">
            <!-- <el-form-item label="IP地址" prop="">
              <el-input v-model="form.netname" disabled placeholder="请输入台网名称" />
            </el-form-item>
            <el-form-item label="子网掩码" prop="">
              <el-input v-model="form.netname" disabled placeholder="请输入台网名称" />
            </el-form-item>
            <el-form-item label="网关" prop="">
              <el-input v-model="form.netname" disabled placeholder="请输入台网名称" />
            </el-form-item>
            <el-form-item label="端口" prop="">
              <el-input v-model="form.netname" disabled placeholder="请输入台网名称" />
            </el-form-item> -->
            <!-- <el-form-item label="天气代码" prop="weatherCode">
              <el-input v-model="form.weatherCode" disabled placeholder="请输入天气代码" />
            </el-form-item> -->
            <el-form-item label="软件版本号" prop="softwareVersion">
              <el-input v-model="form.softwareVersion" disabled placeholder="" />
            </el-form-item>
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="form.longitude" disabled placeholder="请输入经度">
                <template slot="append">度</template>
              </el-input>
            </el-form-item>
            <el-form-item label="纬度" prop="latitude">
              <el-input v-model="form.latitude" disabled placeholder="请输入纬度">
                <template slot="append">度</template>
              </el-input>
            </el-form-item>
            <el-form-item label="站点名称" prop="siteName">
              <el-input v-model="form.siteName" disabled placeholder="请输入站点名称" />
            </el-form-item>
            <el-form-item label="城市编码" prop="cityCode">
              <el-input v-model="form.cityCode" disabled placeholder="请输入城市编码" />
            </el-form-item>
            <el-form-item label="摄像头" prop="isHasCameraLabel">
              <el-input v-model="form.isHasCameraLabel" disabled placeholder="" />
            </el-form-item>
            <el-form-item label="震动传感器" prop="isHasVibrationSensorLabel">
              <el-input v-model="form.isHasVibrationSensorLabel" disabled placeholder="" />
            </el-form-item>
            <el-form-item label="主对时服务器IP" prop="masterServerIp">
              <el-input v-model="form.masterServerIp" disabled placeholder="请输入主对时服务器IP" />
            </el-form-item>
            <!-- <el-form-item label="副对时服务器IP" prop="">
              <el-input v-model="form.netname" disabled placeholder="请输入台网名称" />
            </el-form-item>
            <el-form-item label="对时间隔" prop="">
              <el-input v-model="form.netname" disabled placeholder="请输入台网名称" />
            </el-form-item>
            <el-form-item label="对时延迟" prop="">
              <el-input v-model="form.netname" disabled placeholder="请输入台网名称" />
            </el-form-item>
            <el-form-item label="自动发送" prop="">
              <el-input v-model="form.netname" disabled placeholder="请输入台网名称" />
            </el-form-item> -->
            <el-form-item label="MQTT服务器IP地址" prop="mqttServerIp">
              <el-input v-model="form.mqttServerIp" disabled placeholder="请输入MQTT服务器IP地址" />
            </el-form-item>
            <el-form-item label="MQTT服务器端口号" prop="mqttServerPort">
              <el-input v-model="form.mqttServerPort" disabled placeholder="请输入MQTT服务器端口号" />
            </el-form-item>
            <!-- <el-form-item label="低频" prop="">
              <el-input v-model="form.netname" disabled placeholder="请输入台网名称" />
            </el-form-item>
            <el-form-item label="高频" prop="">
              <el-input v-model="form.netname" disabled placeholder="请输入台网名称" />
            </el-form-item>
            <el-form-item label="触发类型" prop="">
              <el-input v-model="form.netname" disabled placeholder="请输入台网名称" />
            </el-form-item>
            <el-form-item label="LTA时间窗口" prop="">
              <el-input v-model="form.netname" disabled placeholder="请输入台网名称" />
            </el-form-item> -->
            <el-form-item label="FTP服务器地址" prop="ftpServerAddress">
              <el-input v-model="form.ftpServerAddress" disabled placeholder="请输入FTP服务器地址" />
            </el-form-item>
            <el-form-item label="订阅主题" prop="mqttTheme">
              <el-input v-model="form.mqttTheme" disabled placeholder="请输入订阅主题" />
            </el-form-item>
          </div>
        </div>
      </el-form>
    </div>

  </div>
</template>

<script>
import { getDeviceParam } from '@/api/device/device';
export default {
  name: 'parameters',
  props: {
    searchData: {
      type: Object,
      default: () => {
        return {};
      },
    },
    register: {
      type: Boolean,
      default: () => {
        return true
      }
    }
  },
  components: {},
  data() {
    return {
      // 触发模式
      trigFlag: true,
      thFlag: true,
      // 表单校验
      rules: {},
      tabIndex: '0',
      contentStyleObj: {
        height: '520px',
      },
      tabName: 'setOneRef',
      tabs: [
        {
          name: '注册信息',
          refName: 'setOneRef',
        },
        {
          name: '静态信息',
          refName: 'setTwoRef',
        },
      ],
      checkedCities: ['highlight'],
      cities: [
        {
          name: '隐藏相同参数',
          value: 'hidden',
        },
        {
          name: '高亮不同参数',
          value: 'highlight',
        },
      ],
      form: {},
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getList();
    this.getHight();
    window.addEventListener('resize', this.getHight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getHight);
  },
  methods: {
    async getList() {
      let res = await getDeviceParam(this.searchData.deviceId);
      console.log(res);
      if (res.code == 200) {
        this.form = res.data;
      }
    },
    submitForm() {
      this.$emit('out');
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm('form');
    },
    jump(tab, event) {
      let target = document.querySelector('.scroll-content');
      let scrollItems = document.querySelectorAll('.scroll-item');
      // 判断滚动条是否滚动到底部
      if (target.scrollHeight <= target.scrollTop + target.clientHeight) {
        this.tabIndex = tab.index.toString();
      }
      let totalY = scrollItems[tab.index].offsetTop - scrollItems[0].offsetTop; // 锚点元素距离其offsetParent(这里是body)顶部的距离(待滚动的距离)
      let distance = document.querySelector('.scroll-content').scrollTop; // 滚动条距离滚动区域顶部的距离
      // let distance = document.body.scrollTop || document.documentElement.scrollTop || window.pageYOffset // 滚动条距离滚动区域顶部的距离(滚动区域为窗口)
      // 滚动动画实现, 使用setTimeout的递归实现平滑滚动，将距离细分为50小段，10ms滚动一次
      // 计算每一小段的距离
      let step = totalY / 50;
      if (totalY > distance) {
        smoothDown(document.querySelector('.scroll-content'));
      } else {
        let newTotal = distance - totalY;
        step = newTotal / 50;
        smoothUp(document.querySelector('.scroll-content'));
      }

      // 参数element为滚动区域
      function smoothDown(element) {
        if (distance < totalY) {
          distance += step;
          element.scrollTop = distance;
          setTimeout(smoothDown.bind(this, element), 10);
        } else {
          element.scrollTop = totalY;
        }
      }

      // 参数element为滚动区域
      function smoothUp(element) {
        if (distance > totalY) {
          distance -= step;
          element.scrollTop = distance;
          setTimeout(smoothUp.bind(this, element), 10);
        } else {
          element.scrollTop = totalY;
        }
      }
    },
    // 滚动条滚动
    onScroll(e) {
      let scrollItems = document.querySelectorAll('.scroll-item');
      for (let i = scrollItems.length - 1; i >= 0; i--) {
        // 判断滚动条滚动距离是否大于当前滚动项可滚动距离
        let judge =
          e.target.scrollTop >=
          scrollItems[i].offsetTop - scrollItems[0].offsetTop - 100;
        console.log(judge);
        console.log(i);
        if (judge) {
          console.log(this.tabIndex);
          this.tabIndex = i.toString();
          // 找对应的tab-name值
          this.tabName = this.tabs[this.tabIndex].refName;
          break;
        }
      }
    },
    getHight() {
      this.contentStyleObj.height = window.innerHeight - 540 + 'px';
    },
  },
};
</script>
<style lang='scss' scoped>
#param {
  width: 100%;
  height: 100%;
  position: relative;

  .tab {
    position: absolute;
    right: 0;
  }

  .scroll-head {
    // position: absolute;
    width: 636px;
    display: flex;
    height: 85px;
    border-top: 1px solid #f0f0f0;
    border-left: 1px solid #f0f0f0;
    background: #fff;

    >div {
      border-right: 1px solid #f0f0f0;
      border-bottom: 1px solid #f0f0f0;
    }

    >div:nth-child(1) {
      position: relative;
      box-sizing: border-box;
      padding: 20px;
      line-height: 20px;
    }

    >div:nth-child(2),
    >div:nth-child(3),
    >div:nth-child(4) {
      text-indent: 1rem;
      line-height: 85px;
      font-size: 16px;
      font-family: PingFangSC-Medium, PingFang SC;
      font-weight: 600;
      color: #000000;
    }
  }

  .scroll-content {
    position: relative;
    width: 100%;

    // height: 520px;
    .scroll-item {
      width: calc(100% - 20px);
      border-left: 1px solid #f0f0f0;
      border-right: 1px solid #f0f0f0;
      // border-right: 1px solid #f0f0f0;
      border-bottom: 1px solid #f0f0f0;
      margin-bottom: 10px;

      .scroll-item-title {
        position: relative;
        height: 38px;
        border-bottom: 1px solid #f0f0f0;
        border-right: 1px solid #f0f0f0;
        border-top: 1px solid #f0f0f0;

        div {
          position: absolute;
          top: 50%;
          transform: translateY(-50%);
        }

        .scroll-item-title-before {
          left: 16px;
          width: 4px;
          height: 22px;
          background: #1890ff;
        }

        .scroll-item-title-content {
          left: 28px;
        }

        .scroll-item-title-after {
          right: 28px;
        }
      }

      .scroll-item-content {
        padding: 0 20px;
        // > div {
        //   display: flex;
        //   height: 38px;
        //   line-height: 38px;
        //   border-bottom: 1px solid #f0f0f0;
        //   > div {
        //     text-indent: 1rem;
        //     border-right: 1px solid #f0f0f0;
        //   }
        //   > div:last-child {
        //     text-indent: 0rem;
        //     text-align: center;
        //   }
        // }
        // > div:last-child {
        //   border-bottom: none;
        // }
      }
    }
  }

  ::v-deep .el-input {
    width: 320px;
  }

  .scroll-content::-webkit-scrollbar {
    width: 8px;
    /*height: 4px;*/
  }

  .scroll-content::-webkit-scrollbar-thumb {
    border-radius: 4px;
    -webkit-box-shadow: inset 0 0 5px rgba(217, 217, 217, 1);
    background: rgba(0, 0, 0, 0.2);
  }

  .scroll-content::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 5px rgba(240, 240, 240, 1);
    border-radius: 4px;
    background: rgba(0, 0, 0, 0.1);
  }
}</style>